package org.adream.account.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.adream.account.aop.annotation.DataSource;
import org.adream.account.dao.HrmResourceDao;
import org.adream.account.entity.HrmResourceEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.util.Constant;
import org.adream.account.util.UserUtil;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


@Service
@DataSource("oaDataSource")
public class HrmResourceService {
	
	@Autowired
	private HrmResourceDao hrmResourceDao;
	
	private final static Logger logger = LoggerFactory.getLogger(HrmResourceService.class);
	
	/**
	 * 绑定oa账号
	 * @return
	 */
	public ResultModel<String> bindOaAccount(String loginid, HttpServletRequest request) {
		String uid = UserUtil.getUidByRequest(request);
		if(StringUtils.isEmpty(uid)) {
			logger.warn("绑定oa账号失败,登录信息失效:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "登录失效,请刷新重新登录", null);
		}
		//查看uid是否已绑定过oa
		HrmResourceEntity uidHrmResource = hrmResourceDao.queryHrmResourceByTextField3(uid);
		if(uidHrmResource != null) {
			logger.warn("绑定oa账号失败," + uid + "账号已被绑定:(");
			return new ResultModel<String>(ResultModel.HAS_FOUND_OBJECT, "绑定失败,account账号已绑定了相关oa账号", null);
		}
		HrmResourceEntity hrmResource = hrmResourceDao.queryHrmResourceByLoginid(loginid);
		if(hrmResource == null) {
			logger.warn("绑定oa账号失败,账号不存在:(");
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "绑定失败,oa账号不存在", null);
		}
		if(!StringUtils.isEmpty(hrmResource.getTextfield3())) {
			logger.warn("绑定oa账号失败,账号已被绑定:(");
			return new ResultModel<String>(ResultModel.HAS_FOUND_OBJECT, "绑定失败,oa账号已绑定", null);
		}
		//设置oa textfield3字段为cas id
		hrmResource.setTextfield3(uid);
		hrmResourceDao.updateHrmResource(hrmResource);
		return new ResultModel<String>(ResultModel.SUCCESS, "绑定成功", null);
	}
	
	/**
	 * 查询account账号是否已绑定过oa
	 * @param request
	 * @return
	 */
	public ResultModel<String> queryHrmResourceByUid(String uid) {
		HrmResourceEntity hrmResource = hrmResourceDao.queryHrmResourceByTextField3(uid);
		if(hrmResource == null) {
			logger.warn("查询失败,账号不存在:(");
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "查询失败,oa账号不存在", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, hrmResource.getLoginid());
	}
	
	/**
	 * 查询oa账号
	 * @return
	 */
	public ResultModel<List<HrmResourceEntity>> queryHrmResources() {
		List<HrmResourceEntity> hrmResources = hrmResourceDao.queryHrmResources();
		if(Utils.isEmptyCollection(hrmResources)) {
			logger.warn("查询失败,账号不存在:(");
			return new ResultModel<List<HrmResourceEntity>>(ResultModel.NOT_FOUND_OBJECT, "查询失败,oa账号不存在", null);
		}
		return new ResultModel<List<HrmResourceEntity>>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, hrmResources); 
	}
	
	/**
	 * 查询已绑定的oa账号
	 * @return
	 */
	public PageInfo<HrmResourceEntity> queryBoundHrmResources(String loginid,Integer pageNo,Integer pageSize) {
		pageNo = (pageNo == null) ? Constant.ONE : pageNo;
		pageSize = (pageSize == null) ? Constant.TEN: pageSize;
		PageHelper.startPage(pageNo,pageSize);
		List<HrmResourceEntity> boundHrmResources = hrmResourceDao.queryBoundHrmResources(loginid);
		//封装Page对象，每页显示10条记录
		PageInfo<HrmResourceEntity> page = new PageInfo<>(boundHrmResources,Constant.TEN);
		return page;
	}
	
	/**
	 * 解绑oa账号
	 * @param loginid
	 * @return
	 */
	public ResultModel<String> unBindOaAccount(Integer id) {
		HrmResourceEntity hrmResource = hrmResourceDao.queryHrmResourceById(id);
		if(hrmResource == null) {
			logger.warn("解绑失败,oa账号不存在:(");
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "解绑失败,oa账号不存在", null);
		}
		hrmResource.setTextfield3(null);
		hrmResourceDao.updateHrmResource(hrmResource);
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}

	//多数据源没有配置事务，暂且没有事务
	//	解绑oa账号
	public ResultModel<?> frontUnBindOaAccount(String uid) {
		HrmResourceEntity hrmResource = hrmResourceDao.queryHrmResourceByTextField3(uid);
		if (hrmResource != null) hrmResource.setTextfield3(null);
		hrmResourceDao.updateHrmResource(hrmResource);
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
}